﻿
using CSharpLibs.Annotations;

namespace System.IO
{
	[Builtin]
	public class StringReader : TextReader
	{
		// Fields
		private int pos;
		private String s;

		// Methods
		public StringReader(String s)
			: base()
		{
			this.pos = 0;
			this.s = s;
		}

		public override void Close()
		{ ;}

		public override int Peek()
		{
			if(this.pos == this.s.Length)
				return -1;
			
			return this.s[this.pos];
		}

		public override int Read()
		{
			if(this.pos == this.s.Length)
				return -1;
			
			return this.s[this.pos++];
		}

		public override int Read(char[] buffer, int index, int count)
		{
			int num = this.s.Length - this.pos;
			if(num > 0)
			{
				if(num > count)
					num = count;
				
				this.s.CopyTo(this.pos, buffer, index, num);
				this.pos += num;
			}
			return num;
		}

		public override string ReadLine()
		{
			int num = this.pos;
			while(num < this.s.Length)
			{
				char ch = this.s[num];
				switch(ch)
				{
					case '\r':
					case '\n':
						{
							String str = this.s.Substring(this.pos, num - this.pos);
							this.pos = num + 1;
							if(((ch == '\r') && (this.pos < this.s.Length)) && (this.s[this.pos] == '\n'))
							{
								this.pos++;
							}
							return str;
						}
				}
				num++;
			}
			
			if(num > this.pos)
			{
				String str2 = this.s.Substring(this.pos, num - this.pos);
				this.pos = num;
				return str2;
			}

			return null;
		}

		public override string ReadToEnd()
		{
			string str;
			
			if(this.pos == 0)
				str = this.s;
			else
				str = this.s.Substring(this.pos, this.s.Length - this.pos);
			
			this.pos = this.s.Length;
			return str;
		}
	}
}
